//添加客户

interface IClientManagentAddViewModel extends ng.IScope {
    /**
     * 客户单位名称
     */
    name: string;
    /**
     * 客服专员编号
     */
    serviceOfficerId: string;
    /**
     * 客服专员下拉列表
     */
    serviceOfficerIdDropDown: EmployeeListItemResponse[];
    /**
     * 商务专员编号
     */
    businessOfficerId: string;
    /**
     * 商务专员下拉列表
     */
    businessOfficerDropDown: EmployeeListItemResponse[];
    /**
     * 省编号
     */
    provinceCode: string;
    /**
     * 市编号
     */
    cityCode: string;
    /**
     * 区编号
     */
    areaCode: string;
    /**
     * 详细地址
     */
    addess: string;
    /**
     * 管理员账户
     */
    adminUserName: string;
    /**
     * 管理员密码
     */
    adminPassword: string;
    /**
     * 重复管理员密码
     */
    reAdminPassword: string;
    /**
     * 手机号
     */
    phoneNumber: string;
    /**
     * 客户类型
     */
    clientType:string;
    /**
     * 客户类型下拉
     */

    clientTypeList:ValueListItemResponse[]
    /**
     * 产品
     */
    products: string;
    /**
     * 产品包装方式
     */
    productPack: string;
    /**
     * 付款方式
     */
    paidWay: string;
    /**
     * 备注
     */
    remarks: string;
    /**
     * 点击事件
     */
    click: () => void;
}

class ClientManagentAddController {
   
    constructor(private $scope: IClientManagentAddViewModel, private $state: angular.ui.IStateService, private clientService: IClientService, private employeeService: IEmployeeService,
        private $ngBootbox: BootboxService, private valueService) {
        this.init();
    }

    init(): void {
        this.$scope.name = "";
        this.$scope.serviceOfficerId = "";
        this.$scope.serviceOfficerIdDropDown = null;
        this.$scope.businessOfficerId = "";
        this.$scope.businessOfficerDropDown = null;
        this.$scope.provinceCode = "";
        this.$scope.cityCode = "";
        this.$scope.areaCode = "";
        this.$scope.addess = "";
        this.$scope.adminUserName = "";
        this.$scope.adminPassword = "";
        this.$scope.reAdminPassword = "";
        this.$scope.phoneNumber = "";
        this.$scope.products = "";
        this.$scope.productPack = "";
        this.$scope.paidWay = "";
        this.$scope.remarks = "";
        this.$scope.click = this.addClient;
        this.loadServiceOfficerDropDown();
        this.loadBusinessOfficerDropDown();
        this.$scope.clientTypeList = this.valueService.getClientTypeList().data;
    }

    /**
     * 填充客服专员下拉列表
     */
    loadServiceOfficerDropDown(): void {
        this.employeeService.getbytype(CommissionerType.CustomerService.toString(), 0, -1).then(result => {
            this.$scope.serviceOfficerIdDropDown = result.data.data;
        });
    }

    /**
     * 填充商务专员下拉列表
     */
    loadBusinessOfficerDropDown(): void {
        this.employeeService.getbytype(CommissionerType.BusinessAffairs.toString(),0, -1).then(result => {
            this.$scope.businessOfficerDropDown = result.data.data;
        });
    }

    addClient=()=> {
        if (!$("#clientAddForm").valid()) { return };
        this.$ngBootbox.confirm("确定保存此数据吗？").then(e => {
            this.clientService.add(
                this.$scope.name,
                this.$scope.serviceOfficerId,
                this.$scope.businessOfficerId,
                this.$scope.provinceCode,
                this.$scope.cityCode,
                this.$scope.areaCode,
                this.$scope.addess,
                this.$scope.adminUserName,
                this.$scope.adminPassword,
                this.$scope.reAdminPassword,
                this.$scope.phoneNumber,
                this.$scope.clientType,
                this.$scope.products,
                this.$scope.productPack,
                this.$scope.paidWay,
                this.$scope.remarks
            ).then(result => {
                // window.localStorage.clientId=result.data.extData;
                let client = {
                    clientId:result.data.extData,
                    clientName: this.$scope.name
                }
                window.localStorage.setItem("client",JSON.stringify(client));
                if(this.$state.current.name.indexOf('New')>-1){
                    this.$state.go("app.client.client_editNew");  
                }
                else{
                    this.$state.go("app.client.client_edit");  
                }
                       
            });

        });
    }
}

angular.module("tms").controller("clientManagentAddController", ["$scope", "$state", "clientService", "employeeService", "$ngBootbox","valueService", ClientManagentAddController]);